package com.dxyxx.android.setting;

import java.util.Date;

import com.dxyxx.pojo.User;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 登陆页面点击记住密码的时候，用来保存的记录.. 超过30天的记录 必须重新登录。
 * 
 * 一个常见的android内存泄露 问题 见http://xiechengfa.iteye.com/blog/1413688
 * 
 * @author chinesejie
 * 
 */
public class UserPreferences {

	private static UserPreferences up = null;
	private static SharedPreferences.Editor editor = null;
	private static SharedPreferences sp = null;

	private UserPreferences() {
	}

	public static UserPreferences getInstance(Context context) {
		if (up == null) {
			up = new UserPreferences();
			sp = context.getSharedPreferences("user", Context.MODE_PRIVATE); // ---private
																				// 存储
			editor = sp.edit();
		}
		return up;
	}

	//	public User getUser() {
	//		if (sp.getBoolean("remember", false) == true) {
	//			User user = new User(sp.getString("phone", null), sp.getString("password", null), sp.getLong("keepTime", 0), sp.getBoolean("login", false));
	//			return user;
	//		} else {
	//			return null;
	//		}
	//	}

	public User getCurrentUser() {
		User user = new User(sp.getString("phone", null), sp.getString("password", null), sp.getString("image", null), sp.getString("sex", null), null, sp.getLong("keepTime", 0), sp.getBoolean("login", false), sp.getString("username", null));
		user.setImage(sp.getString("image", null));
		try {
			if (sp.getLong("birthday", 0L) != 0) {
				user.setBirthday(new Date(sp.getLong("birthday", 0L)));
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		user.setUsername(sp.getString("username", null));
		return user;

	}

	public void setUser(User user) {
		editor.putBoolean("remember", true);
		editor.putString("phone", user.getPhone());
		editor.putString("password", user.getPassword());
		editor.putString("image", user.getImage());
		editor.putLong("birthday", user.getBirthday().getTime());
		editor.putString("sex", user.getSex());
		editor.putLong("keepTime", user.getKeepTime());
		editor.putBoolean("login", user.isLogin());
		editor.putString("username", user.getUsername());
		editor.commit();
	}

	/**
	 * 更新用户 信息 不包括更新 手机 跟 密码。。
	 * 
	 * @param user
	 */
	public void updateUser(User user) {
		editor.putBoolean("remember", true);
		//		editor.putString("phone", user.getPhone());
		//		editor.putString("password", user.getPassword());
		editor.putLong("keepTime", user.getKeepTime());
		editor.putString("sex", user.getSex());
		if (user.getBirthday() != null) {
			editor.putLong("birthday", user.getBirthday().getTime());
		}
		editor.putBoolean("login", true);
		editor.putString("image", user.getImage());
		editor.putString("username", user.getUsername());//昵称
		editor.putString("name", user.getName());// 真实名字

		editor.commit();
	}

	public void clearUser() {
		editor.putBoolean("remember", false);
		editor.putString("phone", null);
		editor.putString("password", null);
		editor.putLong("keepTime", 0);
		editor.commit();
	}

	public void setToken(String token) {

		editor.putString("token", token);
		editor.commit();
	}

	public void updateToken(String token) {

		editor.putString("token", token);
		editor.commit();
	}

	public String getToken() {
		return sp.getString("token", null);
	}

	public void setPayType(String payType) {
		editor.putString("payType", payType);
		editor.commit();
	}

	public String getPayType() {
		return sp.getString("payType", null);
	}

}
